﻿var columns =
            [
                 { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                { display: '销售单号', name: 'ProductSellBillNo', mintWidth: 80, width: 120 }, //, type: 'int', mintWidth: 40, width: 100
                 {display: '客户端订单号', name: 'ClientOrderNo', mintWidth: 80, width: 120 },
                { display: '销售时间', name: 'SellTime', mintWidth: 180, width: 180, type: 'selfDate' },
              { display: 'BillState', name: 'BillState', hide: 1 },
                { display: 'AuditState', name: 'AuditState', type: 'text', hide: 1 },
               {display: '销售金额(元)', name: 'SellMoney', mintWidth: 80, width: 100 },
               { display: '客户名称', name: 'ClientName', mintWidth: 100, width: 140 },
               { display: '销售人员', name: 'SellerName', mintWidth: 80, width: 100 },
               // { display: '是否结算', name: 'SettleState', mintWidth: 100, width: 140 },
                { display: '送货人', name: 'SenderName', mintWidth: 80, width: 100 },
                { display: '订单状态', name: 'BillStateName', mintWidth: 80, width: 140 },
                { display: '仓库名称', name: 'StorehouseName', mintWidth: 80, width: 140 },
                { display: '制单人', name: 'CreateName', mintWidth: 80, width: 100 },
                 { display: '是否审核', name: 'AuditStateName', mintWidth: 100, width: 140 },
                 { display: '审核时间', name: 'AuditTime', mintWidth: 180, width: 180, type: 'selfDate' },
                  { display: '审核人员', name: 'AuditorName', mintWidth: 80, width: 100 }
             ];

var manager;
//init controls
$(document).ready(function () {
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    initligerGrid();
    initDataControls();
});

function initligerGrid() {
    manager = null;
    manager = $("#ProductOutBillGrid").ligerGrid({
        columns: columns,
        title: '销售管理',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: '99%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                    { text: '增加', click: dialogAdd, icon: 'add' },
                                    { line: true },
                                    { text: '修改', click: modifyOperation, icon: 'modify' },
                                    { line: true },
//                                    { text: '提交', click: confirmOperation, icon: 'modify' },
//                                    { line: true },
                                    { text: '审核', click: auditOperation, icon: 'modify' },
                                     { line: true },
                                      { text: '反审核', click: unAuditOperation, icon: 'modify' },
                                     { line: true },
                                    { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' },
                                    { line: true },
                                    { text: '打印', click: printOperation, img: '../Content/images/print.png' },
                                    { line: true },
                                    { text: '查看', click: dialogDetailView, img: '../Content/images/Search.png' }
                                    ]
        },
        parms: { orgCode: $("#HidOrgCode").val(), billNo: $("#billNo").val(), clientName: $("#clientName").val(), fhName: $("#fhName").val(), StartDate: $("#StartDate").val(), EndDate: $("#EndDate").val() },
        url: "../ProductOut/GetJsonOfProductOutBills?rand=" + Math.random(),
        onDblClickRow: function (data, rowindex, rowobj) { dialogView(data.ID); }
    });
}


//detail
function dialogDetailView() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择要查看的行!'); return; }
    var billId = row["ID"].toString();
    dialogView(billId);
 }
function dialogView(billId) {
    $.ligerDialog.open({ url: "../ProductOut/Detail?id=" + billId, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false });
}

//add
function dialogAdd() {
    $.ligerDialog.open({ url: "../ProductOut/Add?orgCode=" + $("#HidOrgCode").val(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false });
}


//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    $.ligerDialog.confirm('确定要删除吗？', function (yes) {
        if (yes) {
            if (row["BillState"] == 1) { $.ligerDialog.success('已提交，不能删除！'); return; }
            $.get("../ProductOut/Delete?billId=" + row["ID"].toString() + "&ids=all&rand=" + Math.random(), {}, function (data) {
                if (data != undefined && data == "9") {
                    initligerGrid();
                }
            });
        }
    });
}
//update 
function modifyOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要编辑的行!'); return;
    }
    if (row["BillState"].toString() == "1") { $.ligerDialog.success('已提交，不能修改!'); return; }
    $.ligerDialog.open({ url: "../ProductOut/Add?orgCode=" + $("#HidOrgCode").val()+"&id=" + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}


//auditOperation 
function auditOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要审核的行!'); return;
    }
    if (row["BillState"].toString() != "1") { $.ligerDialog.success('未提交，不能审核!'); return; }
    if ( parseInt(row["AuditState"].toString())>0) { $.ligerDialog.success('已审核，不能重复审核!'); return; }
    $.ligerDialog.open({ url: "../ProductOut/Add?orgCode=" + $("#HidOrgCode").val() + "&id=" + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}
//unAuditOperation
function unAuditOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要反审核的行!'); return;
    }
    if (parseInt(row["AuditState"].toString()) <= 0) { $.ligerDialog.success('未审核，不能反审核!'); return; }
    $.ligerDialog.open({ url: "../ProductOut/Add?orgCode=" + $("#HidOrgCode").val() + "&id=" + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}
////auditOperation
//function auditOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) { $.ligerDialog.success('请选择需要审核的行！'); return; }
//    $.ligerDialog.confirm('请选择需要审核该行？', function (yes) {
//        if (yes) {
//            $.get("../ProductOut/Audit?billId=" + row["ID"].toString(), {}, function (data) {
//                if (data != undefined && data == "9") {
//                    initligerGrid();
//                    $.ligerDialog.success('审核成功！');
//                }
//            });
//        }
//    });
//}
////comfirm
//function confirmOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) { $.ligerDialog.success('请选择需要提交的行！'); return; }
//    $.ligerDialog.confirm('请选择需要提交该行？', function (yes) {
//        if (yes) {
//            $.get("../ProductOut/Confirm?billId=" + row["ID"].toString(), {}, function (data) {
//                if (data != undefined && data == "9") {
//                    initligerGrid();
//                    $.ligerDialog.success('提交成功！');
//                }
//            });
//        }
//    });
//}

//print
function printOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要打印的行!'); return;
    }
    if (row["AuditState"].toString() != "1") { $.ligerDialog.success('单据未审核或审核未通过，不能打印!'); return; }
    window.showModalDialog("../ProductOut/Print?billId=" + row["ID"].toString(), window, "scroll:yes;dialogWidth=800;dialogHeight=600");
  //  window.location.href = "../ProductOut/Print?billId=" + row["ID"].toString();
   // prn1_preview(row["ID"].toString());
//    $.get("../ProductOut/Print?billId=" + row["ID"].toString(), {}, function (data) {
//        if (data != undefined && data == "9") {
//            initligerGrid();
//            $.ligerDialog.success('提交成功！');
//        }
//    });
}


//初始化时间
function initDataControls() {
    $("#StartDate").ligerDateEditor({ showTime: true, format: "yyyy-MM-dd", labelWidth: 140, labelAlign: 'left' });
    $("#EndDate").ligerDateEditor({ showTime: true, format: "yyyy-MM-dd", labelWidth: 140, labelAlign: 'left' });
}